<?php
namespace app\api\controller;

use think\Controller;
use app\api\model\Ticket;
use app\api\model\Userticket;

class Ticketss extends Controller
{

    /**
     * 查询所有的优惠券
     */
    public function index()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost()) {
            $ticket = new Ticket();
            $data = $ticket->index();
            AjaxReturn('1', '成功', ['data' => $data]);
        }
    }

    /**
     * 用户抢优惠券
     */
    public function rob()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost()) {
            $ticketid = input('id');
            $userid = input('uid');

            $ticket = new Ticket();
            $data = $ticket->rob($ticketid, $userid);
            $datas['info'] = '抢到了优惠券';
            if ($data == 0) {
                AjaxReturn('10017', '未抢到优惠券');
            } else {
                AjaxReturn('1', '成功', ['details' => $datas]);
            }
        }
    }

    /**
     * 用户查询自己优惠券的详细信息
     */
    public function mytickets()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost()) {
            $userid = input('id');
            $ticket = new Ticket();
            $data = $ticket->mytickets($userid);
            if ($data == 0) {
                AjaxReturn('10018', '未查询到优惠券');
            } else {
                AjaxReturn('1', '成功', ['details' => $data]);
            }
        }
    }

    /**
     * 用户查询自己优惠券的总数
     */
    public function counticket()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost()) {
            $info = [];
            $info['userid'] = input('id');
            $info['status'] = 0;
            $userticket = new Userticket();
            $data = $userticket->counticket($info);
            AjaxReturn('1','成功',['details'=>$data]);
        }
    }
}